package org.elucid.gadmin;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 
 * @author george
 *
 */
public class Utilities {

	/**
	 * 
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public static String readTextFile(File file) throws Exception {
		StringBuffer content = new StringBuffer("");
		FileInputStream fstream = new FileInputStream( file );
		// Get the object of DataInputStream
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		// Read File Line By Line
		while ((strLine = br.readLine()) != null) {
			// Print the content on the console
			content.append(strLine);
			content.append("\n");
		}
		// Close the input stream
		in.close();

		return content.toString();
	}

	/**
	 * 
	 * @param str
	 * @param file
	 * @throws IOException
	 */
	public static void writeToFile(String str, File file) throws IOException {
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		out.write(str);
		out.close();
	}
}
